Ext.define('ZYY.controller.system.employee',{
      extend : 'Ext.app.Controller',
      views:['system.employee','system.deptTree','system.employeeEdit'],
      models:['system.employee'],
      stores:['system.employee','system.deptTree'],
      requires:['Ext.ux.RowExpander','Ext.ux.PagingMemoryProxy','Ext.ux.ProgressBarPager'],
      refs : [
     	  {ref: 'grid', selector: '', xtype: 'employee',  autoCreate: true},
     	  {ref:'queryField',selector:'employee toolbar textfield[name=queryParam]'}
      ],
      init:function(application){
      	  if (this.inited) {
					return;
				}
		  this.inited = true;
		  
		  this.getSystemEmployeeStore().addListener('beforeload',function(){
		  	 var queryField=Ext.ComponentQuery.query("employee toolbar textfield[name=queryParam]")[0];
		  	 if(!queryField.validate())
		  	 	return;
		  	 this.proxy.extraParams['param'] = queryField.getValue();
//		  	  Ext.apply(this.proxy.extraParams, {param:queryField.getValue()});
		  	 var node=Ext.ComponentQuery.query("employee deptTree")[0].getSelectionModel().getSelection()[0];
		  	 if(node)
		  	 	this.proxy.extraParams['deptid'] = node.data.id;
		  });
		  this.control({
		  		"employee deptTree":{
		  			render:function(tree){
   						 tree.expandAll();
      	  			},
      	  			itemclick:this.clickDept
		  		},
	      	  	"employee deptTree tool":{
	      	  		click:this.reloadTree
	      	  	},
	      	  	"employee toolbar button[action=add]":{
	      	  		click:this.add
	      	  	},
	      	  	"employee toolbar button[action=edit]":{
	      	  		click:this.edit
	      	  	},
	      	  	"employee toolbar button[action=delete]":{
	      	  		click:this.del
	      	  	},
	      	  	"employee toolbar button[action=query]":{
	      	  		click:this.query
	      	  	},
	      	  	"employee toolbar button[action=refresh]":{
	      	  		click:this.refresh
	      	  	},
	      	  	"employee toolbar textfield[name=queryParam]":{
	      	  		specialkey:this.queryParam
	      	  	},
	      	  	"employeeEdit  button[action=save]":{
      	  			click:this.saveEmployee
      	  		}
		  })
      },
      actionIndex: function() {
        this.application.setMainView(this.getGrid());
      },
      reloadTree:function(button){
	      	var tree =button.up("panel");
			tree.store.getRootNode().removeAll();
			tree.store.load();
			setTimeout(function() {
						tree.expandAll();
					},200);
	  },
	  clickDept:function(view, record){
	  	 this.getSystemEmployeeStore().getProxy().url=path+'/admin/emp/listByDeptid.htm';
	  	 this.getSystemEmployeeStore().load({
	  	 	params : {
					start : 0,
					limit : 20,
					deptid : record.data.id
				}
	  	 });
	  },
	  add:function(){
	  		var edit=Ext.widget('employeeEdit',{title:"新增人员",iconCls:"user_add"});
                        edit.down('hiddenfield[name=emppass]').setValue('96e79218965eb72c92a549dd5a330112');
	  },
	  edit:function(button){
	  		var edit = Ext.widget('employeeEdit',{title:"修改人员",iconCls:"user_edit"});
	  		var record = button.up('employee').down('gridpanel').getSelectionModel().getSelection()[0];
	  		edit.down('form').loadRecord(record);
	  		console.log(path+record.data.headpic)
	  		$("#headimg").attr("src",path+record.data.headpic);
	  },
	  del:function(button){
	  		var records = button.up('employee').down('gridpanel').getSelectionModel().getSelection();
	  		var store=button.up('employee').down('gridpanel').getStore();
			Ext.MessageBox.confirm('提示', '确认删除选中的人员吗？', function(btn){
				if(btn=="yes"){
				  	var ids=[];
				  	for(var i=0;i<records.length;i++){
				  		ids.push(records[i].data.empid);
				  	}
				  	Ext.Ajax.request({
					    url: path+"/admin/emp/delete.htm",
					    params: {
					        ids:ids.toString()
					    },
					    success: function(response){
					    	var data = Ext.Number.from(response.responseText);
					        if(data>0){
					  			 Ext.MessageBox.alert('提示', "删除成功！");
					  			 store.remove(records);
				  			}
					    }
					});
		    	}
			});
	  },
	  refresh:function(){
	  	 this.getQueryField().setValue('');
	  	 var store=this.getSystemEmployeeStore();
	  	 var node=Ext.ComponentQuery.query("employee deptTree")[0].getSelectionModel().getSelection()[0];
	  	 if(node){
	  	     store.getProxy().url=path+'/admin/emp/listByDeptid.htm';
		  	 store.load({
			  	 params : {
							start : 0,
							limit : 20,
							deptid : node.data.id
						}
		  	 });
	  	 }else{
		  	  store.getProxy().url=path+'/admin/emp/list.htm';
		  	  store.load({
			  	 params : {
							start : 0,
							limit : 20,
							page:1
						}
		  	  });
	  	 }
	  },
	  saveEmployee:function(button){
	  		var win=button.up("window");
	   		var form = win.down('form').getForm();
	   		var url;
	   		if(form.findField("empid").getValue()){
	   			 url =  path+"/admin/emp/update.htm";
	   		}else{
	   			 url =  path+"/admin/emp/add.htm";
	   		}
	   		console.log(form.getValues());
            if(form.isValid()){
            	 form.submit({
                    url:url,
                    success: function(f, o) {
                    	form.reset();
                    	win.close();
                        Ext.MessageBox.alert('提示', "操作成功！");
                    }
                });
            }
	  },
	  query:function(){
	  	 var field=this.getQueryField();
	  	 if(!field.validate())
	  	 	return;
	  	 var store=this.getSystemEmployeeStore();
	  	 store.getProxy().url=path+'/admin/emp/list.htm';
  	 	 store.load({
				params : {
					start : 0,
					limit : 20,
					param : field.getValue()
				}
			});
	  },
	  queryParam:function(field, e){
	  	if (e.getKey() == Ext.EventObject.ENTER) {
		   this.query();
		}
	  }
});